package collection_try;
import java.util.*;

public class LinkedListTest {
    public static void main(String[] args) {
        // 实现List
        System.out.println("List 相关方法------------------------------");
        List<String> listLink = new LinkedList<>();
        listLink.add("add");
        listLink.add("remove");
        listLink.add(1,"add（1，“”）");
        listLink.addAll(Arrays.asList(new String[]{"addAll1","addAll2","addAll1"}));
        System.out.println("add/addAll: "+listLink);

        System.out.println(listLink.remove("remove"));
        System.out.println(listLink.remove(0));
        System.out.println("remove: "+listLink);

        System.out.println(listLink.set(0, "add"));
        System.out.println(listLink);

        System.out.println(listLink.get(1));

        System.out.println(listLink.contains("add"));
        System.out.println(listLink.indexOf("addAll1"));
        System.out.println(listLink.lastIndexOf("addAll1"));

        // 实现Queue
        System.out.println("Queue 相关方法----------------------------");
        Queue<String> queueLink = new LinkedList<>();
        queueLink.offer("offer1");
        queueLink.offer("offer2");
        queueLink.offer("offer3");
        queueLink.offer("offer4");
        System.out.println(queueLink);

        System.out.println(queueLink.poll());
        System.out.println(queueLink);

        System.out.println(queueLink.peek());
        System.out.println(queueLink.element());

        // 实现Deque extends Queue
        // Deque<String> dequeArray = new ArrayDeque<>(); 栈和队列
        System.out.println("Deque 相关方法:----------------------------");
        Deque<String> dequeLink = new LinkedList<>();
        System.out.println("作为stack使用：");
        dequeLink.push("push1");
        dequeLink.push("push2");
        dequeLink.push("push3");
        dequeLink.push("push4");
        System.out.println(dequeLink);

        System.out.println(dequeLink.pop());
        System.out.println(dequeLink);

        System.out.println(dequeLink.peek());
        dequeLink.clear();
        System.out.println(dequeLink);

        System.out.println("作为双向链表使用：");
        dequeLink.addFirst("addFirst");
        dequeLink.addLast("addLast");
        System.out.println(dequeLink);
        System.out.println(dequeLink.getFirst());
        System.out.println(dequeLink.getLast());

        System.out.println(dequeLink.removeFirst());
        System.out.println(dequeLink.removeLast());

        dequeLink.offerFirst("offerFirst");
        dequeLink.offerLast("offerLast");
        System.out.println(dequeLink);
        System.out.println(dequeLink.peekFirst());
        System.out.println(dequeLink.peekLast());

        System.out.println(dequeLink.pollFirst());
        System.out.println(dequeLink.pollLast());



        //set List接口的方法
        LinkedList<String> linkedList = new LinkedList<>(Arrays.asList(new String[]{"apple", "banana", "orange", "strawberry"}));
        System.out.println("set方法返回值测试=="+linkedList.set(1, "set_test"));

        //遍历
        System.out.println("遍历：");
        for(Iterator<String> iterator = linkedList.iterator(); iterator.hasNext();){
            System.out.print(iterator.next()+" ");
        }
        System.out.println();
        for (int i = 0; i < linkedList.size(); i++) {
            System.out.print(linkedList.get(i)+" ");
        }
    }
}
